import configdata from './config'
import cache from './cache'
const md5 = require('./md5.js');
  var now = Date.parse(new Date()); //与header中的Send-Time一致
    var salt = "jtsc";
    var sign = md5.hex(now+salt);//header中X-URETAIL-AUTH的值
module.exports = {
	config: function(name) {
		var info = null;
		if (name) {
			var name2 = name.split("."); //字符分割
			if (name2.length > 1) {
				info = configdata[name2[0]][name2[1]] || null;
			} else {
				info = configdata[name] || null;
			}
			if (info == null) {
				let web_config = cache.get("web_config");
				if (web_config) {
					if (name2.length > 1) {
						info = web_config[name2[0]][name2[1]] || null;
					} else {
						info = web_config[name] || null;
					}
				}
			}
		}
		return info;
	},
	post: function(url, data, header) {
		uni.showLoading({
			title: '加载中'
		})
		header = header || "application/json;charset=UTF-8";
		url = this.config("APIHOST")+url;
		
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "POST",
				header: {
					"content-type": header
				},
				dataType: 'json',
				success: function(result) {
					// console.log(url)
					// console.log(data)
					// console.log(result)
					succ.call(self, result.data)
					 uni.hideLoading();
					if(result.data.code === 900){
						uni.showModal({
							title:'提示',
							content:result.data.message,
							success:function(res){
								if(res.confirm){
									uni.navigateTo({
										url:`/pages/login/login`
									})
								}
								else if (res.cancel) {
								            console.log('用户点击取消');
								        }
							}
						})
					}else if(result.data.code === 999){
						uni.showModal({
							title:'提示',
							content:result.data.message,
							showCancel:false,
							success:function(res){
								if(res.confirm){
									
								}
								
							}
						})
					}
				},
				fail: function(e) {
					console.log(e)
					error.call(self, e)
				}
			})
		})
	},
	get: function(url, data, header) {
		uni.showLoading({
			title: '加载中'
		})
		header = header || "application/json;charset=UTF-8";
		url = this.config("APIHOST")+url;
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "GET",
				header: {
					"content-type": header
				},
				success: function(result) {
					
					 uni.hideLoading();
					succ.call(self, result.data)
				},
				fail: function(e) {
					error.call(self, e)
				}
			})
		})
	},
	post5: function(url, data, header) {
		uni.showLoading({
			title: '加载中'
		})
		header = header || "application/json;charset=UTF-8";
		url = this.config("PAY")+url;
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "POST",
				header: {
					"content-type": header
				},
				success: function(result) {
					succ.call(self, result.data)
					 uni.hideLoading();
					if(result.data.code === 900){
						
						uni.showModal({
							title:'提示',
							content:result.data.message,
							success:function(res){
								if(res.confirm){
									uni.navigateTo({
										url:`/pages/login/login`
									})
								}
								else if (res.cancel) {
								            console.log('用户点击取消');
								        }
							}
						})
					}else if(result.data.code === 999){
						uni.showModal({
							title:'提示',
							content:result.data.message,
							showCancel:false,
							success:function(res){
								if(res.confirm){
									
								}
								
							}
						})
					}
				},
				fail: function(e) {
					error.call(self, e)
				}
			})
		})
	},
	get5: function(url, data, header) {
		uni.showLoading({
			title: '加载中'
		})
		header = header || "application/json;charset=UTF-8";
		url = this.config("PAY")+url;
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "GET",
				header: {
					"content-type": header
				},
				success: function(result) {
					 uni.hideLoading();
					succ.call(self, result.data)
				},
				fail: function(e) {
					error.call(self, e)
				}
			})
		})
	},
	post1: function(url, data, header,sing) {
		uni.showLoading({
			title: '加载中'
		})
		header = header || "application/json;charset=UTF-8";
		url = this.config("USERPATH")+url;
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "POST",
				header: {
					"content-type": header,
					'X-Authorization':sing
				},
				success: function(result) {
					succ.call(self, result.data)
					 uni.hideLoading();
					if(result.data.code === 900){
						uni.showModal({
							title:'提示',
							content:result.data.message,
							success:function(res){
								if(res.confirm){
									uni.navigateTo({
										url:`/pages/login/login`
									})
								}
								else if (res.cancel) {
								            console.log('用户点击取消');
								        }
							}
						})
					}else if(result.data.code === 999){
						uni.showModal({
							title:'提示',
							content:result.data.message,
							showCancel:false,
							success:function(res){
								if(res.confirm){
									
								}
								
							}
						})
					}
				},
				fail: function(e) {
					error.call(self, e)
				}
			})
		})
	},
	get1: function(url, data, header) {
		uni.showLoading({
			title: '加载中'
		})
		header = header || "application/json;charset=UTF-8";
		url = this.config("USERPATH")+url;
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "GET",
				header: {
					"content-type": header
				},
				success: function(result) {
					 uni.hideLoading();
					succ.call(self, result.data)
				},
				fail: function(e) {
					error.call(self, e)
				}
			})
		})
	},
	post3: function(url, data, header,sing) {
		uni.showLoading({
			title: '加载中'
		})
		header = header || "application/json;charset=UTF-8";
		url = this.config("URL")+url;
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "POST",
				header: {
					"content-type": header
				},
				success: function(result) {
					succ.call(self, result.data)
					 uni.hideLoading();
					if(result.data.code === 900){
						uni.showModal({
							title:'提示',
							content:result.data.message,
							success:function(res){
								if(res.confirm){
									uni.navigateTo({
										url:`/pages/login/login`
									})
								}
								else if (res.cancel) {
								            console.log('用户点击取消');
								        }
							}
						})
					}else if(result.data.code === 999){
						uni.showModal({
							title:'提示',
							content:result.data.message,
							showCancel:false,
							success:function(res){
								if(res.confirm){
									
								}
								
							}
						})
					}
				},
				fail: function(e) {
					error.call(self, e)
				}
			})
		})
	},
	get3: function(url, data, header) {
		uni.showLoading({
			title: '加载中'
		})
		header = header || "application/json;charset=UTF-8";
		url = this.config("URL")+url;
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "GET",
				header: {
					"content-type": header
				},
				success: function(result) {
					 uni.hideLoading();
					succ.call(self, result.data)
				},
				fail: function(e) {
					error.call(self, e)
				}
			})
		})
	},
	get4: function(url, data, header) {
		uni.showLoading({
			title: '加载中'
		})
		header = header || "application/json;charset=UTF-8";
		url = this.config("OUT")+url;
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "GET",
				header: {
					"content-type": header
				},
				success: function(result) {
					 uni.hideLoading();
					succ.call(self, result.data)
				},
				fail: function(e) {
					error.call(self, e)
				}
			})
		})
	},
	post4: function(url, data, header,sing) {
		uni.showLoading({
			title: '加载中'
		})
		header = header || "application/json;charset=UTF-8";
		url = this.config("OUT")+url;
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "POST",
				header: {
					"content-type": header,
					'X-URETAIL-AUTH':sign,
					'Send-Time':now
				},
				success: function(result) {
					succ.call(self, result.data)
					 uni.hideLoading();
					if(result.data.code === 900){
						uni.showModal({
							title:'提示',
							content:result.data.message,
							success:function(res){
								if(res.confirm){
									uni.navigateTo({
										url:`/pages/login/login`
									})
								}
								else if (res.cancel) {
								            console.log('用户点击取消');
								        }
							}
						})
					}else if(result.data.code === 0){
						
						// uni.showModal({
						// 	title:'提示',
						// 	content:result.data.message||result.data.msg,
						// 	showCancel:false,
						// 	success:function(res){
						// 		if(res.confirm){
									
						// 		}
								
						// 	}
						// })
					}
				},
				fail: function(e) {
					
					error.call(self, e)
				}
			})
		})
	}
}
